package com.yanjue.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yanjue.bo.LikeSeeBO;
import com.yanjue.common.entity.UserLikeSee;
import com.yanjue.common.util.Result;
import com.yanjue.common.util.ResultUtil;
import com.yanjue.mapper.UserLikeSeeMapper;
import com.yanjue.service.UserLikeSeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 喜欢看接口，，就类似给书点赞添加到喜欢那种
 */
@RestController
@Api("用户喜欢看")
@RequestMapping("account/like-see")
@Slf4j
public class LikeSeeController {

    @Autowired
    private UserLikeSeeService userLikeSeeService;
    @Autowired
    private UserLikeSeeMapper userLikeSeeMapper;

    @ApiOperation(value = "用户喜欢点击接口", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "userId", value = "用户ID", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "bookId", value = "图书ID", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "value", value = "值:0取消喜欢,1:喜欢", required = true, dataType = "int")
    })
    @PostMapping("/click")
    public Result likeSeeClick(@RequestHeader("userId") Integer userId, @RequestBody LikeSeeBO likeSeeBO) {
        String bookId = likeSeeBO.getBookId();
        Integer value = likeSeeBO.getValue();
        return this.userLikeSeeService.likeSeeClick(userId, bookId, value);
    }


    @ApiOperation(value = "获取图书喜欢总数", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "bookId", value = "图书ID", required = true, dataType = "String")
    })
    @GetMapping("/get-count")
    public Result getBookLikesCount(String bookId) {
        return this.userLikeSeeService.getBookLikesCount(bookId);
    }


    /**
     *  获取用户喜欢书单 其实就是获取的book集合分页
     * @param userId
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "获取用户喜欢书单", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "userId", value = "用户ID", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "page", value = "页数", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "limit", value = "每页数量", required = true, dataType = "int")
    })
    @GetMapping("/get-books")
    public Result getUserLikeBookList(@RequestHeader("userId") Integer userId, Integer page, Integer limit) {
        return this.userLikeSeeService.getUserLikeBookList(userId, page, limit);
    }





    @ApiOperation(value = "用户是否喜欢此书", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "userId", value = "用户ID", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "bookId", value = "图书ID", required = true, dataType = "String")
    })
    @GetMapping("/exist-book")
    public Result userLikeThisBook(@RequestHeader("userId") Integer userId, String bookId) {

        Long result = null;
        try {
            result = 0L;
            LambdaQueryWrapper<UserLikeSee> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(UserLikeSee::getUserId,userId)
                    .eq(UserLikeSee::getBookId,bookId);
            result = userLikeSeeMapper.selectCount(queryWrapper);
        } catch (Exception e) {
            //TODO 自定义异常
            log.error("异常");
        }
        return ResultUtil.success(result);
    }
}
